//
//  AllNextDetailView.m
//  DongChiDaChiProject1
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "AllNextDetailView.h"

@implementation AllNextDetailView



// 初始化方法
- (instancetype)initAllNextDetailByFetchArray:(NSArray *)fetchArray AndModel:(AllNextModel *)model
{
    self = [super init];
    if (self) {
        // 构建视图
        [self addViewByArray:fetchArray AndModel:model];
    }
    return self;
}

- (void)addViewByArray:(NSArray *)array AndModel:(AllNextModel *)model
{
    // scrollView
    self.backGroundScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth,  kScreenHeight)];
    self.backGroundScrollView.contentSize = CGSizeMake(kScreenWidth, 2 * kScreenHeight);
    
    self.backGroundScrollView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_backGroundScrollView];
    // 总图片
    self.pictureImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 250 * kWidthFitParameter)];
    self.pictureImageView.backgroundColor = [UIColor whiteColor];
    self.pictureImageView.userInteractionEnabled = YES;
    [self.pictureImageView sd_setImageWithURL:[NSURL URLWithString:model.albums] placeholderImage:nil];
    [self.backGroundScrollView addSubview:_pictureImageView];
    // 新型button
    self.xinXingButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.xinXingButton.frame = CGRectMake(300 * kWidthFitParameter, 180 * kWidthFitParameter, 50 * kWidthFitParameter, 50 * kWidthFitParameter);
    self.xinXingButton.layer.cornerRadius = self.xinXingButton.width / 2;
    self.xinXingButton.layer.masksToBounds = YES;
    [self.xinXingButton setBackgroundImage:[UIImage imageNamed:@"8058b57f365d77c7a52f1e12ca38c482.jpg"] forState:UIControlStateNormal];
    self.xinXingButton.backgroundColor = [UIColor cyanColor];
    [self.pictureImageView addSubview:_xinXingButton];
    // 999
    self.shuZiLabel = [[UILabel alloc] initWithFrame:CGRectMake(335 * kWidthFitParameter, 200 * kWidthFitParameter, 25 * kWidthFitParameter, 25 * kWidthFitParameter)];
    self.shuZiLabel.backgroundColor = [UIColor orangeColor];
    self.shuZiLabel.text = @"999+";
    self.shuZiLabel.layer.cornerRadius = self.shuZiLabel.width / 2;
    self.shuZiLabel.layer.masksToBounds = YES;
    self.shuZiLabel.font = [UIFont systemFontOfSize:9 * kWidthFitParameter];
    self.shuZiLabel.textColor = [UIColor whiteColor];
    [self.pictureImageView addSubview:_shuZiLabel];
    
    // 标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * kWidthFitParameter, self.pictureImageView.bottom + 20 * kWidthFitParameter, 200 * kWidthFitParameter, 40 * kWidthFitParameter)];
    self.titleLabel.font = [UIFont systemFontOfSize:25 * kWidthFitParameter];
    self.titleLabel.text = model.title;
    //self.titleLabel.backgroundColor = [UIColor cyanColor];
    [self.backGroundScrollView addSubview:_titleLabel];
    
    // 小图标
    self.smallPictureImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.titleLabel.left, self.titleLabel.bottom + 20 * kWidthFitParameter, 45 * kWidthFitParameter, 45 * kWidthFitParameter)];
    self.smallPictureImageView.backgroundColor = [UIColor whiteColor];
    self.smallPictureImageView.layer.cornerRadius = self.smallPictureImageView.width / 2;
    [self.smallPictureImageView sd_setImageWithURL:[NSURL URLWithString:model.albums] placeholderImage:nil];
    self.smallPictureImageView.layer.masksToBounds = YES;
    [self.backGroundScrollView addSubview:_smallPictureImageView];
    
    // 营养介绍
    self.yingYangTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.smallPictureImageView.right + 50 * kWidthFitParameter, self.smallPictureImageView.top + 5 * kWidthFitParameter, 150 * kWidthFitParameter, 35)];
    // self.yingYangTitleLabel.backgroundColor = [UIColor cyanColor];
    
    self.yingYangTitleLabel.text = @"营  养  说  明";
    self.yingYangTitleLabel.textColor = [UIColor colorWithHue:1 saturation:0.5 brightness:0.5 alpha:1];
    self.yingYangTitleLabel.font = [UIFont systemFontOfSize:25 * kWidthFitParameter];
    [self.backGroundScrollView addSubview:_yingYangTitleLabel];
    
    // 营养
    self.yingYangLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.left, self.smallPictureImageView.bottom + 30 * kWidthFitParameter, kScreenWidth - 40 * kWidthFitParameter, 50 * kWidthFitParameter)];
    self.yingYangLabel.numberOfLines = 0;
    self.yingYangLabel.font = [UIFont systemFontOfSize:18 * kWidthFitParameter];
    self.yingYangLabel.text = model.imtro;
    //  self.yingYangLabel.backgroundColor = [UIColor cyanColor];
    [self.backGroundScrollView addSubview:_yingYangLabel];
    
    // 食材标题
    self.shiCaiTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.left, self.yingYangLabel.bottom + 35 * kWidthFitParameter, 80 * kWidthFitParameter, 35 * kWidthFitParameter)];
    //  self.shiCaiTitleLabel.backgroundColor = [UIColor cyanColor];
    self.shiCaiTitleLabel.text = @"食材";
    self.shiCaiTitleLabel.textColor = [UIColor colorWithHue:1 saturation:0.5 brightness:0.5 alpha:1];
    self.shiCaiTitleLabel.font = [UIFont systemFontOfSize:25 * kWidthFitParameter];
    [self.backGroundScrollView addSubview:_shiCaiTitleLabel];
    
    // 食材
    self.shiCaiTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.titleLabel.left, self.shiCaiTitleLabel.bottom + 20 * kWidthFitParameter, kScreenWidth - 40 * kWidthFitParameter, 50 * kWidthFitParameter) style:UITableViewStylePlain];
    self.shiCaiTableView.scrollEnabled = NO;
    self.shiCaiTableView.backgroundColor = [UIColor whiteColor];
    [self.backGroundScrollView addSubview:_shiCaiTableView];
    
    // 横线
    self.lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.left, self.shiCaiTableView.bottom + 20 * kWidthFitParameter, kScreenWidth - 40 * kWidthFitParameter, 2 * kWidthFitParameter)];
    self.lineLabel.backgroundColor = [UIColor grayColor];
    [self.backGroundScrollView addSubview:_lineLabel];
    
    
    // 制作步骤
    self.buZhouLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.left, self.lineLabel.bottom + 20 * kWidthFitParameter, 100 * kWidthFitParameter, 35 * kWidthFitParameter)];
    //  self.buZhouLabel.backgroundColor = [UIColor cyanColor];
    self.buZhouLabel.text = @"制作步骤";
    self.buZhouLabel.textColor = [UIColor colorWithHue:1 saturation:0.5 brightness:0.5 alpha:1];
    self.buZhouLabel.font = [UIFont systemFontOfSize:25 * kWidthFitParameter];
    [self.backGroundScrollView addSubview:_buZhouLabel];
    
    // 根据fetcharray动态加载步骤
    for (int i = 0; i < array.count; i++) {
        // 得到一个字典
        NSDictionary *tempDic = array[i];
        // 临时图片
        UIImageView *tempImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.titleLabel.left, self.buZhouLabel.bottom + 20 * kWidthFitParameter + i * 90 * kWidthFitParameter, 100 * kWidthFitParameter, 70 * kWidthFitParameter)];
        tempImage.backgroundColor = [UIColor whiteColor];
        [tempImage sd_setImageWithURL:[NSURL URLWithString:[tempDic valueForKey:@"img"]] placeholderImage:nil];
        tempImage.tag = 3000 + i;
        [self.backGroundScrollView addSubview:tempImage];
        // 临时label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(tempImage.right + 15 * kWidthFitParameter, tempImage.top, 200 * kWidthFitParameter, 70 * kWidthFitParameter)];
        label.backgroundColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:15 * kWidthFitParameter];
        label.text = [tempDic valueForKey:@"step"];
        label.numberOfLines = 0;
        label.tag = 4000 + i;
        [self.backGroundScrollView addSubview:label];
        
        
    }
    
    
    
}

// 自适应高度通过营养和食材cell的数量
- (void)adjustHeightByYingYang:(NSString *)yingYangString AndCountOfCell:(NSInteger)countCell AndFetchArray:(NSInteger) countFetchArray
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18 * kWidthFitParameter] forKey:NSFontAttributeName];
    CGRect frame = [yingYangString boundingRectWithSize:CGSizeMake(kScreenWidth - 40 * kWidthFitParameter, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    CGRect newFrame = self.yingYangLabel.frame;
    newFrame.size.height = frame.size.height;
    self.yingYangLabel.frame = newFrame;
    
    // 重新定义食材标题的高度
    self.shiCaiTitleLabel.frame = CGRectMake(self.shiCaiTitleLabel.left, self.yingYangLabel.bottom + 35 * kWidthFitParameter, self.shiCaiTitleLabel.width, self.shiCaiTitleLabel.height);
    // 重新定义食材table的高度
    self.shiCaiTableView.frame = CGRectMake(self.shiCaiTableView.left, self.shiCaiTitleLabel.bottom + 20 * kWidthFitParameter, self.shiCaiTableView.width, 44 * kWidthFitParameter * countCell);
    // 重新定义lineLabel的高度
    self.lineLabel.frame = CGRectMake(self.lineLabel.left, self.shiCaiTableView.bottom + 20 * kWidthFitParameter, self.lineLabel.width, self.lineLabel.height);
    // 重新定义制作步骤的高度
    self.buZhouLabel.frame = CGRectMake(self.buZhouLabel.left, self.lineLabel.bottom + 20 * kWidthFitParameter, self.buZhouLabel.width, self.buZhouLabel.height);
    // 从新定义制作步骤详情的高度
    for (int i = 0; i < countFetchArray; i++) {
        // 得到图片
        UIImageView *imageV = (UIImageView *)[self viewWithTag:3000 + i];
        imageV.frame = CGRectMake(self.titleLabel.left, self.buZhouLabel.bottom + 20 * kWidthFitParameter + i * 90 * kWidthFitParameter, 100 * kWidthFitParameter, 70 * kWidthFitParameter);
        // 得到标题
        UILabel *label = (UILabel *)[self viewWithTag:4000 + i];
        label.frame = CGRectMake(imageV.right + 15 * kWidthFitParameter, imageV.top, 200 * kWidthFitParameter, 70 * kWidthFitParameter);
    }
    
    
    
    // 得到最后一个图片的高度
    UIImageView *tempImageV = (UIImageView *)[self viewWithTag:3000 + countFetchArray - 1];
    
    // scrollView的高度
    self.backGroundScrollView.contentSize = CGSizeMake(kScreenWidth, tempImageV.bottom + 50 * kWidthFitParameter);
    
    
}



@end
